package com.wasion.pwportable.protocol;

import com.wasion.core.util.ConverterXBaseUtil;
import com.wasion.pwportable.protocol.composer.FrameComopserIF;
import com.wasion.pwportable.protocol.composer.FrameComposerImp;

public class OutFramer {

	FrameComopserIF composer = FrameComposerImp.getInstance();

	public String eraseIndicatorData() {
		String data = "00"; // 清除指示器数据
		return composer.composeFrame(Protocol.ERASE_DATA, data);

	}

	public String eraseIndicatorParams() {
		String data = "00"; // 复位终端数据域
		return composer.composeFrame(Protocol.ERASE_PARAMS, data);

	}

	public String getAirFactoryParams() {
		return composer.composeFrame(Protocol.AIR_FACTORY_PARAMS_RD, "");

	}

	public String getBatteryRD() {
		return composer.composeFrame(Protocol.BATTERY_RD, "");

	}

	public String getCableDataRD() {
		return composer.composeFrame(Protocol.CABLE_DATA_RD, "");

	}

	public String getCableFactoryParams() {
		return composer.composeFrame(Protocol.CABLE_FACTORY_PARAMS_RD, "");

	}

	public String getCapacity2CT() {
		return composer.composeFrame(Protocol.T_CAP2CT_RD);

	}

	public String getCapacityRD() {
		return composer.composeFrame(Protocol.CAPACITY_RD, "");

	}

	public String getChipTemperature() {
		return composer.composeFrame(Protocol.CHIP_TEMP_RD, "");

	}

	public String getCurrentParams() {
		return composer.composeFrame(Protocol.CURRENT_PARAMS_RD, "");

	}

	public String getCurrentRD() {
		return composer.composeFrame(Protocol.CURRENT_RD, "");

	}

	public String getDataFrameFaultCount() {
		return composer.composeFrame(Protocol.DATA_FRAME_FAULT_COUNT_RD, "");

	}

	public String getDeivceFaultCount() {
		return composer.composeFrame(Protocol.DEICE_FAULT_RD, "");

	}

	public String getElectricField() {
		return composer.composeFrame(Protocol.ELECTRIC_FIELD_RD);

	}

	public String getEPROMVersion() {
		return composer.composeFrame(Protocol.EPROM_RD);

	}

	public String getGroundElectricFieldInfo() {
		return composer.composeFrame(Protocol.GROUND_ELECTRIC_FIELD_RD, "");

	}

	public String getGroundFaultRD() {
		return composer.composeFrame(Protocol.GroundFault_RD, "");

	}

	public String getGroundMaxTime() {
		return composer.composeFrame(Protocol.GROUND_MAXTIME_RD, "");

	}

	public String getGroundMutationRD() {
		return composer.composeFrame(Protocol.GROUND_MUTATION_RD, "");

	}

	public String getGroundRise() {

		return composer.composeFrame(Protocol.GROUND_RISE_RD, "");

	}

	public String getGroundSampleInfo() {
		return composer.composeFrame(Protocol.GROUND_SAMPLE_RD, "");

	}

	public String getGroundTempCurrent() {
		return composer.composeFrame(Protocol.GROUND_TEMP_CURRENT_RD, "");

	}

	public String getGroundTempCurrent2() {
		return composer.composeFrame(Protocol.GROUND_TEMP_CURRENT_RD2, "");

	}

	public String getIndicatorAddress() {
		return composer.composeFrame(Protocol.DEV_ADDR_RD);

	}

	public String getIndicatorChannel() {
		return composer.composeFrame(Protocol.CHANNEL_RD);

	}

	public String getIndicatorFile(String iAddress) {
		String data = "03" + iAddress; // 按地址查询
		return composer.composeFrame(Protocol.NETWORK_FILE_RD, data);

	}

	public String getIndicatorFileBymPiont(String data) {
		return composer.composeFrame(Protocol.NETWORK_FILE_RD, data);

	}

	public String getIndicatorSleepTime() {
		return composer.composeFrame(Protocol.SLEEP_TIME_RD);

	}

	public String getIndicatorTempVavle() {
		return composer.composeFrame(Protocol.TEMP_VAVLE_RD, "");

	}

	public String getIndicatorTime() {
		return composer.composeFrame(Protocol.INDI_TIME_RD);

	}

	public String getIndicatorVersion() {
		return composer.composeFrame(Protocol.DEV_INFO_RD);

	}

	public String getLastGroundTime() {
		return composer.composeFrame(Protocol.LAST_GROUND_TIME_RD, "");

	}

	public String getLastMetering() {
		return composer.composeFrame(Protocol.T_LAST_GATHER);

	}

	public String getLastShortCurrent() {
		return composer.composeFrame(Protocol.LAST_SHORT_CURRENT_RD, "");

	}

	public String getLastShortInfo() {
		return composer.composeFrame(Protocol.LAST_SHORT_INFO_RD, "");

	}

	public String getLineFaultCount() {
		return composer.composeFrame(Protocol.LINE_FAULT_RD, "");

	}

	public String getMagneticRD() {
		return composer.composeFrame(Protocol.MAGNETIC_RD, "");

	}

	public String getModuleMode() {
		return composer.composeFrame(Protocol.MODULE_MODE_RD, "");

	}

	public String getNetworkStatus() {
		return composer.composeFrame(Protocol.NETWORK_STATUS);
	}

	public String getOverHeatRD() {
		return composer.composeFrame(Protocol.OverHeat_RD, "");

	}

	/**
	 * @param String
	 * @return
	 */
	public String getParamsString(String String) {
		String paramsString = "";

		int[] params = new int[11];

		for (int i = 0; i < params.length; i++) {
			String data = Integer.toHexString(params[i]);
			while (data.length() < 4) {
				data = "0" + data;
			}

			paramsString = paramsString + data.substring(2, 4)
					+ data.substring(0, 2);
		}
		return paramsString;
	}

	public String getRSSIRD() {
		return composer.composeFrame(Protocol.RSSI_RD, "");

	}

	public String getShortCutParams() {
		return composer.composeFrame(Protocol.RUNNING_PARAM_RD, "");

	}

	public String getShortElectricField() {
		return composer.composeFrame(Protocol.SHORT_ELECTRIC_FIELD_RD, "");

	}

	public String getShortFaultRD() {
		return composer.composeFrame(Protocol.ShortFault_RD, "");

	}

	public String getShortFaultStatus() {
		return composer.composeFrame(Protocol.SHORT_FAULT_STATUS_RD, "");

	}

	public String getShortSample() {
		return composer.composeFrame(Protocol.SHORT_SAMPLE_RD, "");

	}

	public String getStartBits() {
		return composer.composeFrame(Protocol.START_BITS_RD, "");

	}

	public String getStartupCurrent() {

		return composer.composeFrame(Protocol.STARTUP_CURRENT_RD, "");

	}

	public String getStartupParams() {
		return composer.composeFrame(Protocol.STARTUP_PARAMS_RD, "");

	}

	public String getStatusRD() {
		return composer.composeFrame(Protocol.STATUS_RD, "");

	}

	public String getTermAddress() {
		return composer.composeFrame(Protocol.T_AREA_CODE_RD);

	}

	public String getTermCommControl() {
		return composer.composeFrame(Protocol.T_COMM_CONTROL_RD);

	}

	public String getTerminalCommInfo() {

		return composer.composeFrame(Protocol.T_COMMUNICATIONS_RD);

	}

	public String getTerminalUser() {
		return composer.composeFrame(Protocol.T_USER_PASSWOR_RD);

	}

	public String getVersion() {
		return composer.composeFrame(Protocol.DEVICE_IDENTIFIER);
	}

	public String getTerminaRegStatus() {
		return composer.composeFrame(Protocol.REG_STATUS_RD);

	}

	public String getTermTime() {
		return composer.composeFrame(Protocol.TERMINAL_TIME_RD);

	}

	public String getTermVersion() {
		return composer.composeFrame(Protocol.T_Version_RD);

	}

	private String getTimeAndModeIn16TextForFlip(int time, int mode, int unit) {

		// 翻牌复归时间（位组合）
		// Bit 0~5 复归时间
		// Bit 6 复归单位 1 :H 0: M
		// Bit 7 复归方式 1 :自动复归 0: 按延时复归
		String timeBits = "000000";
		String unitBit = "0";
		String modeBit = "0";
		String backBackTimeBits = "00000000";

		// 复归时间
		if (mode == 1) {

			modeBit = "1";
			backBackTimeBits = modeBit + unitBit + timeBits;
		}

		else if (mode == 0) {
			timeBits = Integer.toBinaryString(time);
			while (timeBits.length() < 6) {
				timeBits = "0" + timeBits;
			}

			if (unit == 1) {
				unitBit = "1";
			}

			backBackTimeBits = modeBit + unitBit + timeBits;
		}

		int temp = Integer.parseInt(backBackTimeBits, 2);

		String timeAndMode16 = Integer.toHexString(temp).toUpperCase();

		while (timeAndMode16.length() < 2) {
			timeAndMode16 = "0" + timeAndMode16;
		}

		return timeAndMode16;
	}

	public String getTrivalCurrentParams() {
		return composer.composeFrame(Protocol.TRIVAL_CURRENT_PARAMS_RD, "");

	}

	private String getTrivalCurrentStart(int current2a) {
		String trivalCurrentStart;
		if (current2a == 1)
			trivalCurrentStart = "01";
		else
			trivalCurrentStart = "00";
		return trivalCurrentStart;
	}

	public String getZeroCurrentParams() {
		return composer.composeFrame(Protocol.ZERO_CURRENT_PARAMS_RD, "");

	}

	public String getZeroSequenceParams() {
		return composer.composeFrame(Protocol.ZERO_SEQUENCE_RD, "");

	}

	private String integerToSingle16Text(int data) throws NumberFormatException {
		String tmp_16 = Integer.toHexString(data).toUpperCase();
		while (tmp_16.length() < 2) {
			tmp_16 = "0" + tmp_16;
		}
		return tmp_16;
	}

	/**
	 * @param data
	 * @return
	 * @throws NumberFormatException
	 */
	private String intToDouble16Text(int data) throws NumberFormatException {
		String tmp_16 = Integer.toHexString(data).toUpperCase();

		while (tmp_16.length() < 4) {
			tmp_16 = "0" + tmp_16;
		}

		String tmp_16Reverse = tmp_16.substring(2, 4) + tmp_16.substring(0, 2);
		return tmp_16Reverse;
	}

	/*
	 * long to 4 bytes for Proudction time
	 */
	private String longTo4Bytes16Text(long data) {
		String tmp_16 = Long.toHexString(data).toUpperCase();

		while (tmp_16.length() < 8) {
			tmp_16 = "0" + tmp_16;
		}
		return tmp_16;
	}

	public String rebootIndicator() {
		return composer.composeFrame(Protocol.REBOOT_IDICATOR,
				Protocol.REBOOT_IDICATOR_DATA);

	}

	public String rebootTerminal() {
		String data = "00"; // 复位终端数据域
		return composer.composeFrame(Protocol.TERMINAL_RESET, data);

	}

	// public String reStartNetworkStatus() {
	// return composer.composeFrame(Protocol.NETWORK_SET,
	// Protocol.NETWORK_DATA_RESTART); // 重启组网命令
	//
	// }

	public String setAirFactoryParams(Result result) {

		String cutOffCurrent = integerToSingle16Text(result.getShortCut()
				/ Protocol.CURRENT_CAL_REF);

		String cutOffTime = intToDouble16Text(result.getShortCutTime());

		String overLoadCurrent = integerToSingle16Text(result
				.getShortOverFlow() / Protocol.CURRENT_CAL_REF);

		String overLoadTime = intToDouble16Text(result.getShortOverFlowTime());

		int time = result.getTurnBackTime();
		int unit = result.getTurnBackUnit();
		int mode = result.getTurnBackMode();

		String flipTimeAndMode = getTimeAndModeIn16TextForFlip(time, mode, unit);

		String groundedCurrentDetal = integerToSingle16Text(result
				.getGroundRise());

		String groundedMaxTime = intToDouble16Text(result.getGroundMaxTime());

		String trivalCurrentStart = "00";
		trivalCurrentStart = getTrivalCurrentStart(result.getStart2A());

		String sleepTime = integerToSingle16Text(result.getSleepTime());

		String electricField = integerToSingle16Text(result
				.getElectricFieldValve());

		String eFieldDownTime = integerToSingle16Text(result
				.geteFieldDownTime());

		String eFieldRate = integerToSingle16Text(result.geteFieldDownRate());

		String producedDate = longTo4Bytes16Text(result.getProductTime());

		String dataBytes = cutOffCurrent + cutOffTime + overLoadCurrent
				+ overLoadTime + flipTimeAndMode + groundedCurrentDetal
				+ groundedMaxTime + trivalCurrentStart + sleepTime
				+ electricField + eFieldDownTime + eFieldRate + producedDate;

		return composer.composeFrame(Protocol.AIR_FACTORY_PARAMS_WR, dataBytes);

	}

	public String setCableFactoryParams(Result result) {

		String cutOffCurrent = integerToSingle16Text(result.getShortCut()
				/ Protocol.CURRENT_CAL_REF);

		String cutOffTime = intToDouble16Text(result.getShortCutTime());

		String overLoadCurrent = integerToSingle16Text(result
				.getShortOverFlow() / Protocol.CURRENT_CAL_REF);

		String overLoadTime = intToDouble16Text(result.getShortOverFlowTime());

		int time = result.getTurnBackTime();
		int unit = result.getTurnBackUnit();
		int mode = result.getTurnBackMode();

		String flipTimeAndMode = getTimeAndModeIn16TextForFlip(time, mode, unit);

		// Printer.out("flipTimeAndMode = " + flipTimeAndMode);

		String groundedCurrentDetal = integerToSingle16Text(result
				.getGroundCut());

		String groundedMaxTime = intToDouble16Text(result.getGroundCutTime());

		String groundedOverFlow = integerToSingle16Text(result
				.getGroundOverFlow());

		String groundedOverFlowTime = intToDouble16Text(result
				.getGroundOverFlowTime());

		String tempValve = integerToSingle16Text(result.getTempValve());
		// Printer.out("tempValve = " + tempValve);
		String sleepTime = integerToSingle16Text(result.getSleepTime());
		// Printer.out("sleepTime = " + sleepTime);
		String producedDate = longTo4Bytes16Text(result.getProductTime());
		// Printer.out("producedDate = " + producedDate);

		String dataBytes = cutOffCurrent + cutOffTime + overLoadCurrent
				+ overLoadTime + flipTimeAndMode + groundedCurrentDetal
				+ groundedMaxTime + groundedOverFlow + groundedOverFlowTime
				+ tempValve + sleepTime + producedDate;

		// Printer.out("dataBytes = " + dataBytes);

		return composer.composeFrame(Protocol.CABLE_FACTORY_PARAMS_WR,
				dataBytes);

	}

	public String setCurrentParams(String String) {
		String paramsString = getParamsString(String);

		return composer.composeFrame(Protocol.CURRENT_PARAMS_WR, paramsString);

	}

	public String setElectricField(Result result) {
		String vavle16 = Integer.toHexString(result.getElectricFieldValve());
		if (vavle16.length() < 2) {
			vavle16 = "0" + vavle16;
		}
		String time16 = Integer.toHexString(result.geteFieldDownTime());
		if (time16.length() < 2) {
			time16 = "0" + time16;
		}
		String rate16 = Integer.toHexString(result.geteFieldDownRate());
		if (rate16.length() < 2) {
			rate16 = "0" + rate16;
		}

		String data = vavle16 + time16 + rate16;

		return composer.composeFrame(Protocol.ELECTRIC_FIELD_WR, data);

	}

	public String setGroundMaxTime(String data) {
		String time = Integer.toHexString(Integer.parseInt(data));
		while (time.length() < 4) {
			time = "0" + time;
		}
		String groundMaxTime16 = time.substring(2, 4) + time.substring(0, 2);

		return composer.composeFrame(Protocol.GROUND_MAXTIME_WR,
				groundMaxTime16);

	}

	public String setGroundRise(String data) {
		String current = Integer.toHexString(Integer.parseInt(data));
		while (current.length() < 2) {
			current = "0" + current;
		}
		return composer.composeFrame(Protocol.GROUND_RISE_WR, current);

	}

	public String setIndicatorAddress(String data) {
		String address = data.substring(4, 6) + data.substring(2, 4)
				+ data.substring(0, 2);
		return composer.composeFrame(Protocol.DEV_ADDR_WR, address);

	}

	public String setIndicatorChannel(int data) {
		String channel = Integer.toHexString(data);
		while (channel.length() < 2) {
			channel = "0" + channel;
		}
		return composer.composeFrame(Protocol.CHANNEL_WR, channel);

	}

	public String setIndicatorSleepTime(String data) {

		String time = "";
		if (data.equalsIgnoreCase("1")) {
			time = "01"; // 1s
		} else {
			time = "14"; // 20s
		}
		return composer.composeFrame(Protocol.SLEEP_TIME_WR, time);

	}

	public String setIndicatorTempVavle(String data) {
		String valve16 = Integer.toHexString(Integer.parseInt(data));
		while (valve16.length() < 2) {
			valve16 = "0" + valve16;
		}
		return composer.composeFrame(Protocol.TEMP_VAVLE_WR, valve16);
	}

	public String setIndicatorTime(Long time) {

		String time16 = Long.toHexString(time);
		while (time16.length() < 8) {
			time16 = "0" + time16;
		}
		return composer.composeFrame(Protocol.INDI_TIME_WR, time16);
	}

	public String setModuleMode(int mode) {
		return Protocol.modeCommands[mode];
	}

	public String setRFChannels(int channel) {
		return composer.getChannelCommand(channel);
	}

	public String setShortCutParams(Result result) {
		String cut16 = Integer.toHexString(result.getShortCut()
				/ Protocol.CUTOFF_CURRENTREF);
		while (cut16.length() < 2) {
			cut16 = "0" + cut16;
		}

		String time16 = Integer.toHexString(result.getShortCutTime());
		while (time16.length() < 4) {
			time16 = "0" + time16;
		}
		String cutTime16 = time16.substring(2, 4) + time16.substring(0, 2);

		String overFlow16 = Integer.toHexString(result.getShortOverFlow()
				/ Protocol.CUTOFF_CURRENTREF);
		while (overFlow16.length() < 2) {
			overFlow16 = "0" + overFlow16;
		}

		String overTime = Integer.toHexString(result.getShortOverFlowTime());
		while (overTime.length() < 4) {
			overTime = "0" + overTime;
		}
		String overTime16 = overTime.substring(2, 4) + overTime.substring(0, 2);

		// 复归时间
		String timeBits = "000000";
		String unitBit = "0";
		String modeBit = "1";
		String backBackTimeBits;

		if (result.getTurnBackMode() == 1) {

			backBackTimeBits = "10000000";
			// System.out.println("//should be 10000000 = " + backBackTimeBits);
		}

		else {

			int turnBackTime = result.getTurnBackTime();
			modeBit = "0"; // 延时模式

			if (result.getTurnBackUnit() == 1) {
				unitBit = "1";
			} else {
				unitBit = "0";
			}
			timeBits = Integer.toBinaryString(turnBackTime);
			while (timeBits.length() < 6) {
				timeBits = "0" + timeBits;
			}
		}

		backBackTimeBits = modeBit + unitBit + timeBits;
		int temp = Integer.parseInt(backBackTimeBits, 2);

		String turnBackTime16 = Integer.toHexString(temp).toUpperCase();

		while (turnBackTime16.length() < 2) {
			turnBackTime16 = "0" + turnBackTime16;
		}
		String data = cut16 + cutTime16 + overFlow16 + overTime16
				+ turnBackTime16;
		return composer.composeFrame(Protocol.RUNNING_PARAM_WR, data);

	}

	public String setStartBits(Result result) {

		int start2A = result.getStart2A();
		int start0A = result.getStart0A();
		int tx = result.getTxLed();
		int rx = result.getRxLed();

		String mode = rx + "" + tx + "" + start0A + "" + start2A + "";

		int temp = Integer.parseInt(mode, 2);
		String mode16 = Integer.toHexString(temp).toUpperCase();

		while (mode16.length() < 2) {
			mode16 = "0" + mode16;
		}

		return composer.composeFrame(Protocol.START_BITS_WR, mode16);

	}

	public String setStartupCurrent(String data) {
		String current = Integer.toHexString(Integer.parseInt(data));
		while (current.length() < 4) {
			current = "0" + current;
		}
		String current16 = current.substring(2, 4) + current.substring(0, 2);
		return composer.composeFrame(Protocol.STARTUP_CURRENT_WR, current16);

	}

	public String setStartupParams(Result result) {

		String startupCurrent = integerToSingle16Text(result
				.getStartupCurrent());

		String fault = integerToSingle16Text(result.getFaultMaxTime());

		String cutoff = integerToSingle16Text(result.getCutoffMinTime());

		String dataBytes = startupCurrent + fault + cutoff;

		return composer.composeFrame(Protocol.STARTUP_PARAMS_WR, dataBytes);

	}

	public String setTermAddress(String data) {
		return composer.composeFrame(Protocol.T_AREA_CODE_WR, data);
	}

	public String setTermCommControl(String data) {
		return composer.composeFrame(Protocol.T_COMM_CONTROL_WR, data);

	}

	public String setTerminalCommInfo(Result result) {

		// 主站ip
		String ip = result.getHostIP().trim();

		String[] ips = ip.split("\\."); // split 方法 要注意 \\.

		String ip16[] = new String[4];
		for (int i = 0; i < 4; i++) {
			ip16[i] = Integer.toHexString(Integer.parseInt(ips[i]));
			if (ip16[i].length() == 1) {
				ip16[i] = "0" + ip16[i];
			}
		}
		String ips16 = ip16[0] + ip16[1] + ip16[2] + ip16[3];

		// 主站端口号
		int port = Integer.parseInt(result.getHostPort());
		String port16 = Integer.toHexString(port);
		while (port16.length() < 4) {
			port16 = "0" + port16;
		}
		port16 = port16.substring(2, 4) + port16.substring(0, 2);

		// APN
		String apn16 = ConverterXBaseUtil.encode(result.getApn());
		while (apn16.length() < 32) {
			apn16 = apn16 + "0";
		}

		// 心跳周期

		String heart16 = Integer.toHexString(result.getHeartBeat());
		while (heart16.length() < 2) {
			heart16 = "0" + heart16;
		}
		// 连接方式 UDP /TCP
		String connect16 = "";
		if (result.getConnection() == 0)
			connect16 = "00"; // udp
		else
			connect16 = "01"; // tcp

		String data = ips16 + port16 + apn16 + heart16 + connect16;

		return composer.composeFrame(Protocol.T_COMMUNICATIONS_WR, data);

	}

	public String setTerminalUser(Result result) {

		String user16 = ConverterXBaseUtil.encode(result.getUserName());
		while (user16.length() < 32) {
			user16 = user16 + "0";
		}

		String pass16 = ConverterXBaseUtil.encode(result.getPassword());
		while (pass16.length() < 32) {
			pass16 = pass16 + "0";
		}
		String data = user16 + pass16;

		return composer.composeFrame(Protocol.T_USER_PASSWOR_WR, data);

	}

	public String setTermTime(String time) {

		// 格式 "yyMMdd hh:mm:ss"
		String raw1 = time.replaceAll(":", "");

		String raw2 = raw1.replaceAll(" ", "");

		String data = raw2.substring(10, 12) + raw2.substring(8, 10)
				+ raw2.substring(6, 8) + raw2.substring(4, 6)
				+ raw2.substring(2, 4) + raw2.substring(0, 2);

		return composer.composeFrame(Protocol.TERMINAL_TIME_WR, data);

	}

	public String setTrivalCurrentParams(String String) {
		String paramsString = getParamsString(String);
		return composer.composeFrame(Protocol.TRIVAL_CURRENT_PARAMS_RD,
				paramsString);

	}

	public String setZeroCurrentParams(String String) {
		String paramsString = getParamsString(String);

		return composer.composeFrame(Protocol.ZERO_CURRENT_PARAMS_WR,
				paramsString);

	}

	public String setZeroSequenceParams(Result result) {

		String groundCut16 = Integer.toHexString(result.getGroundCut()
				/ Protocol.GROUND_CUT_CURRENTREF);
		while (groundCut16.length() < 2) {
			groundCut16 = "0" + groundCut16;
		}

		String time16 = Integer.toHexString(result.getGroundCutTime());
		while (time16.length() < 4) {
			time16 = "0" + time16;
		}
		String groundCutTime16 = time16.substring(2, 4)
				+ time16.substring(0, 2);

		String groundOverFlow16 = Integer.toHexString(result
				.getGroundOverFlow() / Protocol.GROUND_CUT_CURRENTREF);
		while (groundOverFlow16.length() < 2) {
			groundOverFlow16 = "0" + groundOverFlow16;
		}

		String overTime16 = Integer.toHexString(result.getGroundOverFlowTime());
		while (overTime16.length() < 4) {
			overTime16 = "0" + overTime16;
		}
		String groundOverTime16 = overTime16.substring(2, 4)
				+ overTime16.substring(0, 2);

		String data = groundCut16 + groundCutTime16 + groundOverFlow16
				+ groundOverTime16;

		return composer.composeFrame(Protocol.ZERO_SEQUENCE_WR, data);

	}

	public String startNetwork() {
		return composer.composeFrame(Protocol.NETWORK_SET,
				Protocol.NETWORK_DATA_START); // 启动组网命令

	}

	// public String reStartNetwork() {
	// return composer.composeFrame(Protocol.NETWORK_SET,
	// Protocol.NETWORK_DATA_RESTART); // 启动组网命令
	//
	// }

	public String stopNetwork() {

		return composer.composeFrame(Protocol.NETWORK_SET,
				Protocol.NETWORK_DATA_STOP); // 停止组网命令

	}

	public String writesNetworkFiles(String data) {
		return composer.composeFrame(Protocol.NETWORK_FILE_WR, data);
	}

	public String getNeighborsInfo() {
		return composer.composeFrame(Protocol.NEIGHBOR_INFO_RD);
	}

	public String setNeighborsInfo(Result result) {

		String neighbor1 = result.getNeighbor1();
		String neighbor2 = result.getNeighbor2();

		String phaseBit;
		if (result.getPhase().equalsIgnoreCase("A")) {
			phaseBit = "1"; // 0001
		} else if (result.getPhase().equalsIgnoreCase("B")) {
			phaseBit = "2"; // 0010
		} else {
			phaseBit = "3"; // 0011
		}

		int branchId = result.getBrachId();

		String branch = Integer.toHexString(branchId);
		String branchByte = phaseBit + branch;
		String data = Protocol.toRealAddress(neighbor1)
				+ Protocol.toRealAddress(neighbor2) + branchByte;

		return composer.composeFrame(Protocol.NEIGHBOR_INFO_WR, data);
	}

	public String getRelayInfo() {
		return composer.composeFrame(Protocol.RELAY_INFO_RD);
	}

	public String setRelayInfo(Result result) {

		String neighbor1 = result.getNeighbor1();
		String neighbor2 = result.getNeighbor2();
		String parent = result.getParent();

		String maxRelayLevel = "0" + result.getMaxRelayLevel();
		String currRelayLevel = "0" + result.getCurrRelayLevel();

		String phaseBit;
		if (result.getPhase().equalsIgnoreCase("A")) {
			phaseBit = "01"; // 0001
		} else if (result.getPhase().equalsIgnoreCase("B")) {
			phaseBit = "02"; // 0010
		} else {
			phaseBit = "03"; // 0011
		}

		String data = Protocol.toRealAddress(neighbor1)
				+ Protocol.toRealAddress(neighbor2)
				+ Protocol.toRealAddress(parent) + maxRelayLevel
				+ currRelayLevel + phaseBit;

		return composer.composeFrame(Protocol.RELAY_INFO_WR, data);
	}

	public String flip(String data) {

		return composer.composeFrame(Protocol.FLIP_ACTION, data);
	}

	public String getRelayRTStatus(String[] nodes) {

		return composer.makeRoutedStatus(nodes);
	}

	/**
	 * 
	 * @Title: beginFCIData
	 * @Description: 开始信号标识
	 * @param
	 * @return
	 * @throws
	 */
	public String beginFCIData() {

		return composer.composeFrame(Protocol.DATA_FCI_TIME_BEGIN);
	}

	/**
	 * 
	 * @Title: getLoadCurrent
	 * @Description: 架空型故障指示器负荷电流数据记录
	 * @param actionId
	 *            B0-BF C0-CF D0-DF
	 * @return
	 * @throws
	 */
	public String getLoadCurrent(String actionId) {

		return composer.composeFrame(actionId);
	}

	/**
	 * 
	 * @Title: getInnerFrame
	 * @Description: 获取内部应用交互帧
	 * @param
	 * @return
	 * @throws
	 */
	public String getInnerFrame(String actionId, String data) {

		return composer.composeInnerFrame(actionId, data);
	}
}
